import { api } from '@/utils/request'

export interface MarketOverview {
  marketCap: number
  volume24h: number
  circulatingSupply: number
  totalSupply: number
  rank: number
}

export interface TrendData {
  date: string
  rewards: number
}

export interface NearPriceData {
  price: number
  change24h: number
  changeRate24h: number
  lastUpdate: number
}

class MarketApiService {
  /**
   * 获取市场概览数据
   */
  async getMarketOverview(): Promise<MarketOverview> {
    try {
      const response = await api.get<MarketOverview>('/api/market/overview')
      return response.data
    } catch (error) {
      console.error('시장 개요 조회 실패:', error)
      throw new Error('시장 데이터를 불러오지 못했습니다')
    }
  }

  /**
   * 获取收益趋势数据
   */
  async getRewardsTrend(days: number = 30): Promise<TrendData[]> {
    try {
      const response = await api.get<TrendData[]>('/api/market/trends', {
        params: { days }
      })
      return response.data
    } catch (error) {
      console.error('수익 추세 조회 실패:', error)
      throw new Error('추세 데이터를 불러오지 못했습니다')
    }
  }

  /**
   * 获取NEAR当前价格
   */
  async getNearPrice(): Promise<NearPriceData> {
    try {
      const response = await api.get<NearPriceData>('/api/market/near-price')
      return response.data
    } catch (error) {
      console.error('NEAR 가격 조회 실패:', error)
      throw new Error('NEAR 가격을 불러오지 못했습니다')
    }
  }
}

export const marketApiService = new MarketApiService()
